package com.casework

import org.dbunit.database.ForwardOnlyResultSetTableFactory
import org.dbunit.database.DatabaseConfig
import org.dbunit.database.DatabaseConnection
import org.dbunit.database.IDatabaseConnection
import org.dbunit.dataset.IDataSet
import org.dbunit.database.search.TablesDependencyHelper
import org.dbunit.dataset.xml.FlatXmlDataSet
import org.dbunit.dataset.FilteredDataSet
import org.dbunit.database.DatabaseSequenceFilter
import org.dbunit.dataset.filter.ITableFilter

class ExportDBService {

    def dataSource
    def connection

    void exportDB() {
        println "dataSource $dataSource"
        println "connection $connection"

        IDatabaseConnection connection = new DatabaseConnection(dataSource.connection);
        DatabaseConfig config = connection.getConfig();

        config.setProperty(DatabaseConfig.PROPERTY_RESULTSET_TABLE_FACTORY, new ForwardOnlyResultSetTableFactory());

        // full database export
//        IDataSet fullDataSet = connection.createDataSet();
//        FlatXmlDataSet.write(fullDataSet, new FileOutputStream("full.xml"));


        ITableFilter filter = new DatabaseSequenceFilter(dataSource.connection);
        IDataSet dataset = new FilteredDataSet(filter, dataSource.connection.createDataSet());

        FlatXmlDataSet.write(dataset, new File("full.xml"));
    }
}
